package com.android.djlmapp.activity.kaika;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.djlmapp.R;
import com.android.djlmapp.application.MyApplication;
import com.android.djlmapp.model.home.Info;
import com.android.readcard.dialog.MyUsualyDialog;
import com.google.gson.Gson;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FuKaBanLiActivity extends Activity {

    @BindView(R.id.tv_jt_title)
    TextView tvJtTitle;
    @BindView(R.id.kaika_iv_close)
    ImageView kaikaIvClose;
    @BindView(R.id.tv_jt_name)
    TextView tvJtName;
    @BindView(R.id.ed_jtbm)
    EditText edJtbm;
    @BindView(R.id.ll_edit)
    LinearLayout llEdit;
    @BindView(R.id.view_line)
    View viewLine;
    @BindView(R.id.tv_tishi)
    TextView tvTishi;
    @BindView(R.id.ll_tishi)
    LinearLayout llTishi;
    @BindView(R.id.ll_parent)
    LinearLayout llParent;
    @BindView(R.id.ll_blfk)
    LinearLayout llBlfk;
    private String keyvalue;
    private MyUsualyDialog myUsualyDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fuka);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    protected void initView() {

        llTishi.setVisibility(View.GONE);
        llBlfk.setVisibility(View.VISIBLE);
        viewLine.setVisibility(View.VISIBLE);
    }

    protected void initData() {
        myUsualyDialog = new MyUsualyDialog(FuKaBanLiActivity.this, com.android.readcard.R.layout.wait_item);
        myUsualyDialog.setCanceledOnTouchOutside(true);
        myUsualyDialog.setCancelable(false);
    }

    private boolean isSure = false;

    @OnClick({R.id.kaika_iv_close, R.id.tv_cancle, R.id.tv_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.kaika_iv_close:

            case R.id.tv_cancle:
                finish();
                break;
            case R.id.tv_sure:
                if (isSure) {
                    Intent intent = new Intent();
                    intent.putExtra("JTBM_RESULT", keyvalue);
                    intent.putExtra("ADDPARA_RESULT", "");
                    setResult(RESULT_OK, intent);
                    finish();
                    overridePendingTransition(R.anim.push_left_in, R.anim.push_right_out);
                } else {
                    viewLine.setVisibility(View.GONE);
                    llEdit.setVisibility(View.GONE);
                    llBlfk.setVisibility(View.GONE);
                    llParent.setVisibility(View.INVISIBLE);

                    keyvalue = edJtbm.getText().toString().trim();
                    myUsualyDialog.show();
                    MyApplication.getInstance().getHttpTools().checkFuKa(keyvalue, new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            try {
                                Gson gson = new Gson();
                                Info info = gson.fromJson(response.body(), Info.class);
                                if (info != null) {
                                    if (info.getCode() == 200) {
                                        isSure = true;
                                        viewLine.setVisibility(View.VISIBLE);
                                        llBlfk.setVisibility(View.VISIBLE);
                                        errShow(info.getMsg());
                                    } else {
                                        errShow(info.getMsg());
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                                Log.e("项目异常", "------Exception------" + e.toString());
                                errShow("数据解析异常");
                            }
                        }

                        @Override
                        public void onError(Response<String> response) {
                            super.onError(response);
                            errShow("网络或服务器错误");
                        }

                        @Override
                        public void onFinish() {
                            super.onFinish();
                            llParent.setVisibility(View.VISIBLE);
                            myUsualyDialog.dismiss();
                        }
                    });
                }
                break;
        }
    }

    private void errShow(String err) {
        llTishi.setVisibility(View.VISIBLE);
        tvTishi.setText(err);

    }
}
